<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animation</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .ball {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            border: 1px solid #000;
            background-color: red;
        }
        title{
            height: 200px;
        }
        @keyframes spread {
            0% {
                transform: scale(0);
                opacity: 1;
            }

            100% {
                transform: scale(1);
                opacity: 0;
            }
        }
    </style>
</head>

<body>
    <h1>Web Animation API</h1>
    <center> center </center>
    <title>title</title>
    <div class="ball"></div>
</body>
<script>
    const ball = document.querySelector('.ball')
    function init() {
        const x = window.innerWidth / 2
        const y = window.innerHeight / 2
        ball.style.transform = `translate(${x}px,${y}px)`
    }
    init()
    window.addEventListener('click', (e) => {
        const x = e.clientX
        const y = e.clientY
        move(x, y)
    })
    function move(x, y) {
        // Animation API
        // ball.animate(keyframes[],options{})
        const ballRect = ball.getBoundingClientRect()
        const initX = ballRect.left + ballRect.width / 2
        const initY = ballRect.top + ballRect.height / 2
        const rad = Math.atan2(y - initY, x - initX)
        const angle = rad * 180 / Math.PI
        ball.animate(
            [
                {
                     transform: `translate(${initX}px,${initY}px) rotate(${angle}deg)`,
                     easing:'ease-out'
                },
                {
                    transform:`translate(${initX}px,${initY}px) rotate(${angle}deg) scaleX(1.5)`,
                    offset:0.6
                },
                {
                    transform:`translate(${x}px,${y}px) rotate(${angle}deg) scaleX(1.5)`,
                    offset:0.8,
                    easing:'ease-in'
                },
                {
                     transform: `translate(${x}px,${y}px) rotate(${angle}deg)` 
                },
            ], 
            {
                duration: 1000,
                fill:'forwards',
            }
        )
    }
</script>

</html>